/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#include "trf_parser.h"
#include <stdio.h>
#include <zlib.h>


//-----------------------------------------------------------------------//
const long time_stamp::since_year = 2000;

const long time_stamp::months[] = {
                                      0,
                                      31,  59,  90,
                                      120, 151, 181,
                                      212, 243, 273,
                                      304, 334,
                                  };


//-----------------------------------------------------------------------//
cfile::~cfile()
{
    close();
}


//-----------------------------------------------------------------------//
void cfile::open( const char *name )
{
    if( ptr != NULL )
        close();
    ptr = (void*)fopen( name, "r" );

    if( ptr == NULL )
    {
        THROW( e_open, "Can't open [RAW] file! {%s}", name );
    }
}


//-----------------------------------------------------------------------//
void cfile::close()
{
    if( ptr != NULL )
    {
        fclose( (FILE*)ptr );
        ptr = NULL;
    }
}


//-----------------------------------------------------------------------//
int cfile::eof()
{
    if( ptr != NULL )
        return feof( (FILE*)ptr );
    return 1;
}


//-----------------------------------------------------------------------//
int cfile::gets( char *text, int max_len )
{
    int ret_val = eof();

    if( ptr != NULL )
    {
        fgets( text, max_len, (FILE*)ptr );
    }

    return ret_val;
}




//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
cgzfile::~cgzfile()
{
    close();
}


//-----------------------------------------------------------------------//
void cgzfile::open( const char *name )
{
    if( ptr != NULL )
        close();
    ptr = (gzFile)gzopen( name, "rb" );

    if( ptr == NULL )
    {
        THROW( e_open, "Can't open [gzip] file! {%s}", name );
    }

}


//-----------------------------------------------------------------------//
void cgzfile::close()
{
    if( ptr != NULL )
    {
        gzclose( (gzFile)ptr );
        ptr = NULL;
    }
}


//-----------------------------------------------------------------------//
int cgzfile::eof()
{
    if( ptr != NULL )
    {
        return gzeof( (gzFile)ptr );
    }

    return 1;
}


//-----------------------------------------------------------------------//
int cgzfile::gets( char *text, int max_len )
{
    int ret_val = eof();

    if( ptr != NULL )
    {
        gzgets( (gzFile)ptr, text, max_len );
    }

    return ret_val;
}
